#include"ManageGame.h"
#include "resource.h"
#include "dom.h"	// for script loading
#include "globals.h"

//these are all the tools of the class there are more functions
// in gameUpdate
ManageGame::ManageGame()
{
	m_hWnd = NULL;
	pGraphics = Graphics::Engine();
}
//creates window simple but needed
void ManageGame::createWindow(int nCmdShow)
{
	//opens window
	ManageGame::OpenWindow("D3DWindow", "40k Tower Defense", nCmdShow);

	/* Setup a Rect with the window dimensions to make sure they have been set */
	/* Setup a Rect with the window dimensions to make sure  they have been set */
	RECT rClientRect;
	if(!::GetClientRect(m_hWnd, &rClientRect))
		throw "GetClientRect() failed";
	//dont really need this
	m_ScreenHeight = (int)(rClientRect.bottom - rClientRect.top);
	m_ScreenWidth = (int)(rClientRect.right - rClientRect.left);
}
//inits all engines made
void ManageGame::init_allEngines()
{
	// Please stick to this order of initalization - M. Alstad

	// Game->Init()
	Graphics::init();
	pGraphics = Graphics::Engine();
	pGraphics->Init(m_hWnd, DIRECTX);
	soundMgr.initSoundFiles(SOUND_MANGER, m_hWnd);
	GUI::Engine()->Init();
	// AI->Init()
}

HWND ManageGame::OpenWindow(const char* cszWinClassName, const char* cszWindowName, int nCmdShow )
{
	WNDCLASSEX wc;

	HINSTANCE hInstance = ::GetModuleHandle (NULL);
	assert (NULL != hInstance);
	HICON hIcon = NULL;
	hIcon = ::LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
	assert(NULL != hIcon);

	wc.lpszClassName	= cszWinClassName;
	wc.cbSize			= sizeof (WNDCLASSEX);
	wc.style			= CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc		= WindowProc;
	wc.hInstance		= hInstance;
	wc.hIcon			= hIcon;
	wc.hIconSm			= hIcon;
	wc.hCursor			= ::LoadCursor (NULL, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH)::GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName		= NULL;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;

	if (0 == RegisterClassEx(&wc))
		return 0;


	m_hWnd = CreateWindow(cszWinClassName,
		cszWindowName,
		WS_OVERLAPPEDWINDOW,
		0, 0, 
		m_ScreenWidth, m_ScreenHeight,
		NULL, NULL, hInstance, NULL);

	//change this so it will be full screen
	ShowWindow (m_hWnd, SW_SHOWMAXIMIZED);//nCmdShow
	UpdateWindow (m_hWnd);
	return m_hWnd;
}

//release the memory
void ManageGame::Release()
{
	// Please stick to this order of releasing - M. Alstad

	// AI->Release();w
	// UI->Release();
	pGraphics->Release();
	// Game->Release();
}

void printDom(DOM * dom, int indent)
{
	for(int i = 0; i < dom->getVarCount(); ++i)
	{
		for(int in = 0; in < indent; ++in)
			g_console->Print(' ');
		g_console->Print(dom->getName(i));
		g_console->Print(" = ");
		g_console->Print(dom->getVar(i));
		g_console->Print('\n');
	}
	for(int i = 0; i < dom->getSubCount(); ++i)
	{
		for(int in = 0; in < indent; ++in)
			g_console->Print(' ');
		g_console->Print('[');
		g_console->Print(dom->getSubName(i));
		g_console->Print("]\n");
		printDom(dom->getSub(i), indent+2);
	}
}

void ManageGame::initGame()
{
	//was going to have a switch statement for different levels
	loadLevel(0);
	//DOM dom;
	//dom.load("Resources\\scripts\\titlescreen.txt");
	//printDom(&dom, 0);
	//	// TODO make some kind of display list, or other organizeing structure to keep track of non-image resources, like script names, coordinates, etc...
	//Add starting textures here
	/*BaseObject * Skull = new BaseObject();
	Skull->setFileName("Resources\\art\\servoskull.bmp");
	point j;
	j.x =600;
	j.y=9;
	Skull->setLoc(j);

	pGraphics->Add(Skull,0);*/

}

void ManageGame::HandleMessages()
{	////////////////////////////////////////////////
	//Any input that you want to do, you can do it here!
	//Any questions on how to access certain bits - skype Tyler Lawton 9/19/11
	////////////////////////////////////////////////
	if(Windows_Input::Engine()->ifMessage())
	{
		MSG CurrMessage;
		CurrMessage = Windows_Input::Engine()->getMessage();
		switch(CurrMessage.message)
		{
		case (WM_KEYDOWN):
			{
				switch(CurrMessage.wParam)
				{
				case VK_F:
					{
						MessageBox(m_hWnd, "F was Pressed", "Yay!", MB_OK);
					
						break;
					}
				case VK_E:
					{
						MessageBox(m_hWnd, "E was Pressed", "Yay!", MB_OK);
						
						break;
					}
				case VK_M:
					{
						soundMgr.stopSound(0);
						break;
					}
				case VK_A:
					{
						soundMgr.playSoundLoop(0);
						break;
					}
				}
				break;
			}
		case (WM_LBUTTONDOWN):
			{
				int x = CurrMessage.pt.x;
				int y = CurrMessage.pt.y;
				LPPOINT CurrentPoint = &CurrMessage.pt;
				ScreenToClient(m_hWnd, CurrentPoint);
				GUI::Engine()->CheckCollision(CurrentPoint->x,CurrentPoint->y);
				
			
				//
				//point s,r,j;
				////this is just mess to have rotation and scaling
				////if (wParam ==MK_LBUTTON)
				////{
				//char text[50];
				//sprintf_s(text, 50,"%d %d", x, y);

				////MessageBox(hWnd, text, "Mouse", MB_OK);
				//BaseObject * Skull = new BaseObject();
				////	Skull->SetAssetIndex("Resources\\art\\servoskull.bmp");
				////random just to mess around	erfgg
				//s.x=1;
				//s.y=1;
				//r.x=0;

				///*TODO: Fix Warnings with int to float conversions*/
				//j.x =x;
				//j.y=y;
				////will be use later 
				//Skull->setLoc(j);
				//Skull->setRot(r);
				//Skull->setScale(s);
				//g_app.getGraphics()->AddObject(Skull,0,0);


			
				break;
			}
		case WM_LBUTTONUP :
			{
				//gets the mouse position
				//int x = CurrMessage.pt.x;
				//int y = CurrMessage.pt.y;
				//point s,r,j;
				////this is just mess to have rotation and scaling
				////if (wParam ==MK_LBUTTON)
				////{
				//char text[50];
				//sprintf_s(text, 50,"%d %d", x, y);

				////MessageBox(hWnd, text, "Mouse", MB_OK);
				//BaseObject * Skull = new BaseObject();
				////	Skull->SetAssetIndex("Resources\\art\\servoskull.bmp");
				////random just to mess around	erfgg
				//s.x=1;
				//s.y=1;
				//r.x=0;

				//j.x =x;
				//j.y=y;
				////will be use later 
				//Skull->setLoc(j);
				//Skull->setRot(r);
				//Skull->setScale(s);
				//g_app.getGraphics()->AddObject(Skull,0,0);


			}break;
			case(WM_RBUTTONUP):
			{
			//	//this is just mess to have rotation and scaling
			//	
			//	//gets the mouse position
			//int x = CurrMessage.pt.x;
			//	int y = CurrMessage.pt.y;
			//	point s,r,j;

			//		char text[50];
			//		sprintf_s(text, 50,"%d %d", x, y);

			//		//MessageBox(hWnd, text, "Mouse", MB_OK);
			//		BaseObject * Skull = new BaseObject();
			//		//Skull->setFileName("Resources\\art\\servoskull.bmp");
			//		
			//		j.x =x;
			//		j.y=y;
			//		
			//		s.x=2;
			//		s.y=2;
			//		r.x=45;

			//		Skull->setLoc(j);
			//		Skull->setRot(r);
			//		Skull->setScale(s);
			//		g_app.getGraphics()->AddObject(Skull,0,0);


			}break;
		default:
			int a = 0;
			break;
		}
		Windows_Input::Engine()->MessageUsed();
	}
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	//Any system critical messages should be handled here, such as hard exit and quit message.
	// Basically, there is no real need to touch this winproc - Tyler Lawton 9/19/11
	switch(uMsg)
	{
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		} break;
		//TODO just messing around so we have stuff on screen input guy can change this around
		//Done, look up! -Tyler Lawton 9/19/2011
		//case WM_LBUTTONUP :
		//	{
		//		//gets the mouse position
		//		int x = LOWORD(lParam);
		//		int y = HIWORD(lParam);
		//		point s,r,j;
		//			//this is just mess to have rotation and scaling
		//		//if (wParam ==MK_LBUTTON)
		//		//{
		//			char text[50];
		//			sprintf_s(text, 50,"%d %d", x, y);

		//			//MessageBox(hWnd, text, "Mouse", MB_OK);
		//			BaseObject * Skull = new BaseObject();
		//			Skull->setFileName("Resources\\art\\servoskull.bmp");
		//			//random just to mess around	erfgg
		//			s.x=1;
		//			s.y=1;
		//			r.x=0;

		//			j.x =x;
		//			j.y=y;
		//			//will be use later 
		//			Skull->setLoc(j);
		//			Skull->setRot(r);
		//			Skull->setScale(s);
		//			g_app.getGraphics()->AddObject(Skull,0);

		//	//	}
		//	
		//	}break;
		//	case(WM_RBUTTONUP):
		//	{
		//		//this is just mess to have rotation and scaling
		//		
		//		//gets the mouse position
		//		int x = LOWORD(lParam);
		//		int y = HIWORD(lParam);
		//		point s,r,j;

		//			char text[50];
		//			sprintf_s(text, 50,"%d %d", x, y);

		//			//MessageBox(hWnd, text, "Mouse", MB_OK);
		//			BaseObject * Skull = new BaseObject();
		//			Skull->setFileName("Resources\\art\\servoskull.bmp");
		//			
		//			j.x =x;
		//			j.y=y;
		//			
		//			s.x=2;
		//			s.y=2;
		//			r.x=45;

		//			Skull->setLoc(j);
		//			Skull->setRot(r);
		//			Skull->setScale(s);
		//			g_app.getGraphics()->AddObject(Skull,0);


		//	}break;
	case(WM_KEYDOWN):
		{
			switch(wParam)
			{

				//Escape Key
			case VK_ESCAPE:
				{
					PostQuitMessage(0);
					break;
				}
			}break;
		}

	}
	return DefWindowProc (hWnd, uMsg, wParam, lParam);
}

